<template>
  <a-modal
    title="批发设置"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    okText=""
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item label="票据抬头">
          <a-input
            placeholder="一般为公司名称"
            v-decorator="['head', { initialValue: '' }]"
          />
        </a-form-item>
        <a-form-item label="票据结尾" style="margin-bottom: 0;">
          <a-textarea
            rows="4"
            placeholder="一般为联系方式及收款账号"
            v-decorator="['tail', {initialValue: '' }]"
          />
          <a-form-item style="margin-bottom: 0;" name="info">
            <a-checkbox-group>
              <a-row>
                <a-col :span="24">
                  <a-checkbox v-decorator="['remark']"> 显示备注信息 </a-checkbox>
                </a-col>
                <a-col :span="24">
                  <a-checkbox v-decorator="['up_down']"> 显示上欠下欠 </a-checkbox>
                </a-col>
              </a-row>
            </a-checkbox-group>
          </a-form-item>
        </a-form-item>
        <a-form-item label="字号">
          <a-select v-decorator="['font_size', { initialValue: '1' }]" placeholder="请选择">
            <a-select-option value="1">小</a-select-option>
            <a-select-option value="2">中</a-select-option>
            <a-select-option value="3">大</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
// 表单字段
const fields = ['head', 'tail', 'remark', 'up_down', 'font_size']
export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      form: this.$form.createForm(this),
    }
  },
  created() {
    console.log('custom modal created')
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
}
</script>
